package com.nowcoder.code.hjnc;

import com.algorithm.model.ListNode;

import java.util.HashMap;

public class NC4 {

    public static void main(String[] args) {

    }


    public boolean hasCycle(ListNode head) {
        ListNode fast = head, slow = head;
        while (fast != null) {
            if (fast.next == null) {
                return false;
            }
            slow = slow.next;
            fast = fast.next.next;
            if (fast == slow) {
                return true;
            }
        }
        return false;
    }

    public boolean hasCycle1(ListNode head) {
        HashMap<ListNode, Object> map = new HashMap<>();
        while (head != null) {
            if (map.containsKey(head)) {
                return true;
            }
            map.put(head, null);
            head = head.next;
        }
        return false;
    }


}
